package com.dex4d.ui.wallet.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dex4d.R;
import com.dex4d.core.Web2Activity;
import com.dex4d.ui.wallet.model.WalletTx;
import com.framework.bigdecimalutils.DoubleFormatTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者    LiQingyi
 * 时间    2018/11/26 10:24
 * 文件    dex4d-android
 * 描述
 */
public class WalletTxAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<WalletTx> list;

    Activity activity;
    LayoutInflater inflater;

    Map<String, WalletTx> txMap;

    public WalletTxAdapter(Activity activity) {
        this.activity = activity;
        this.inflater = LayoutInflater.from(activity);
        this.list = new ArrayList<>();
        this.txMap = new HashMap<>();
    }

    public void addData(WalletTx tx) {
        this.list.add(tx);
        notifyDataSetChanged();
    }

    public void addAll(List<WalletTx> txList, int index) {

        if (txList == null || txList.isEmpty()) return;

        if (index == 0) {
            this.list.clear();
        }

        int size = txList.size();

        for (int i = 0; i < size; i++) {
            WalletTx tx = txList.get(i);
            txMap.put(tx.txHash, tx);
            tx.position = i;
            this.list.add(tx);
        }
        notifyItemRangeChanged(index, size);
    }

    public void updateAll(List<WalletTx> txList) {
        if (txList == null || txList.isEmpty()) return;
        for (WalletTx tx : txList) {
            WalletTx walletTx = txMap.get(tx.txHash);
            walletTx.txReceiptStatus = tx.txReceiptStatus;
            notifyItemChanged(walletTx.position, walletTx);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.item_wallet_tx, viewGroup, false);
        return new TxViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        TxViewHolder vh = (TxViewHolder) viewHolder;
        final WalletTx tx = this.list.get(i);

        String balanceAmount = TextUtils.isEmpty(tx.balance)? "0":DoubleFormatTool.round(tx.balance, 6);

        String balanceStr = String
                .format(activity.getResources().getString(R.string.wallet_tx_item_balance), balanceAmount);

        SpannableStringBuilder builder = new SpannableStringBuilder(balanceStr);
        ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);
        builder.setSpan(whiteSpan, balanceStr.length() - balanceAmount.length(), balanceStr.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);


        vh.txt_balance.setText(builder);

        if (tx.cashflow == 0) {
            vh.txt_cashflow.setText(String.format(activity.getResources().getString(R.string.wallet_tx_item_outcome), tx.symbol, tx.getTxReceiptStatus(activity)));
            vh.txt_amount.setText("-" + DoubleFormatTool.round(tx.value, 6));
            int color = activity.getResources().getColor(R.color.account_token_down);
            vh.txt_amount.setTextColor(color);
        } else {
            vh.txt_cashflow.setText(String.format(activity.getResources().getString(R.string.wallet_tx_item_income), tx.symbol, tx.getTxReceiptStatus(activity)));
            vh.txt_amount.setText("+" + DoubleFormatTool.round(tx.value, 6));
            int color = activity.getResources().getColor(R.color.account_token_up);
            vh.txt_amount.setTextColor(color);
        }
        vh.txt_timestamp.setText(tx.getTxTimeStamp());

        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Web2Activity.openWeb2(activity,tx.getWeb2Url());
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);

        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {

            TxViewHolder vh = (TxViewHolder) holder;
            WalletTx tx = this.list.get(position);

            if (tx.cashflow == 0) {
                vh.txt_cashflow.setText(String.format(activity.getResources().getString(R.string.wallet_tx_item_outcome), tx.symbol, tx.getTxReceiptStatus(activity)));
            } else {
                vh.txt_cashflow.setText(String.format(activity.getResources().getString(R.string.wallet_tx_item_income), tx.symbol, tx.getTxReceiptStatus(activity)));
            }
        }

    }

    @Override
    public int getItemCount() {
        return this.list.size();
    }


    static class TxViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.txt_cashflow)
        TextView txt_cashflow;

        @BindView(R.id.txt_timestamp)
        TextView txt_timestamp;

        @BindView(R.id.txt_amount)
        TextView txt_amount;

        @BindView(R.id.txt_balance)
        TextView txt_balance;

        public TxViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
